<?php

require_once 'mp3/PDO.php';
require_once 'mp3/GestionarUsuari.php';
require_once 'mp3/GestionarCanco.php';

class Mp3_GestionarPlaylist
{
    public $playlistid;
    public $titol;
    public $xml;
    public $cancons = array();
    public $userid;
	public $username;
	protected $target_path = 'C:/wamp/www/tardes/playlists/';
    
	const SQL_PLAYLIST = ' SELECT * 
                    		FROM playlists
                    		WHERE playlistid = :playlistid';
	const SQL_PLAYLISTS_USUARI = ' SELECT *
	                    		FROM playlists
	                    		WHERE userid = :userid';
	const SQL_PLAYLISTS_XML = ' SELECT playlistid
		                    	FROM playlists
		                    	WHERE xml = :xml';
	const SQL_PLAYLISTS_UPDATE = 'UPDATE playlists
								SET titol = :titol, xml = :xml, userid = :userid
								WHERE playlistid = :playlistid';
	const SQL_PLAYLISTS_INSERT = 'INSERT INTO playlists
										(titol, xml, userid)
								VALUES (:titol, :xml, :userid)';
	const SQL_PLAYLISTS_DELETE ='  DELETE
	                        	FROM playlists
	                        	WHERE playlistid = :playlistid';
	
    public function __construct ($playlistid = NULL) {
		if($playlistid != NULL){    	
	        $dbStatement = mp3_PDO::getInstance()->prepare(mp3_GestionarPlaylist::SQL_PLAYLIST); 
	        $dbStatement->execute( array('playlistid' => $playlistid ) );
	        $result = $dbStatement->fetch(PDO::FETCH_ASSOC);
	        if( $result ) {
	            foreach ( $result as $attr => $value ) {
	                $this->$attr = $value;
	            }
	        }
		}
    }
    
    
	public static function allPlaylists( $userid ) {
        $dbStatement = mp3_PDO::getInstance()->prepare( mp3_GestionarPlaylist::SQL_PLAYLISTS_USUARI );
        $dbStatement->execute( array('userid' => $userid ) );
        $result = $dbStatement->fetchAll(PDO::FETCH_ASSOC);
        return $result;       
    }
	
	public static function findByXML( $xml ) {
        $dbStatement = mp3_PDO::getInstance()->prepare( mp3_GestionarPlaylist::SQL_PLAYLISTS_XML );
        $dbStatement->execute( array('xml' => $xml ) );
        $result = $dbStatement->fetch(PDO::FETCH_ASSOC);
        $playlistid = $result['playlistid'];
        return new Playlist($playlistid);        
    }
    
    public function update () {
        if($this->playlistid!==NULL) {
            $dbStatement = mp3_PDO::getInstance()->prepare( Mp3_GestionarPlaylist::SQL_PLAYLISTS_UPDATE );
			$res = $dbStatement->execute( array('titol'         => $this->titol, 
                                        'xml'        	=> $this->xml, 
                                        'userid'		=> $this->userid,
                                        'playlistid'    => $this->playlistid) );
            }
    }
        
	public function insert () {
        if($this->playlistid===NULL) {
            $dbStatement = mp3_PDO::getInstance()->prepare( Mp3_GestionarPlaylist::SQL_PLAYLISTS_INSERT );
			$res = $dbStatement->execute( array('titol'         => $this->titol,
										'xml'         	=> $this->xml,
                                        'userid'		=> $this->userid) );
        }
    }
	
	public function delete() {
        if($this->playlistid!==NULL) {
            $dbStatement = mp3_PDO::getInstance()->prepare( Mp3_GestionarPlaylist::SQL_PLAYLISTS_DELETE );
			$dbStatement->execute( array('playlistid' => $this->playlistid ) );
			unlink($this->target_path . $this->xml .'.xspf');
        }
    }
	
	public function loadXML() {
		$file = $this->target_path . $this->xml .'.xspf';
		$xmlstr = file_get_contents($file);
		$xml = new SimpleXMLElement($xmlstr);
		$this->titol = (string) $xml->titol;
		$this->username = (string) $xml->creator;
		$userId = mp3_GestionarUsuari::cercarPerUsername($this->username);
		$this->userid = $userId;
		foreach ($xml->trackList->children() as $track) {
			$file = str_replace('/tardes/cancons/', '', 
								$track->location);		 
			$canco = new Mp3_GestionarCanco(); 
			$canco->cercarPerFitxer( $file );
            $this->cancons[$canco->id] = $canco;
		}
    }
    
    public function saveXML()
    {
		$string = "<playlist version='1' xmlns='http://xspf.org/ns/0/'>";
		$string .= '<title>'.$this->titol.'</title>';
		$string .= '<creator>'.$this->username.'</creator>';
		$string .= '<trackList>';
			foreach ($this->cancons as $canco) {		
				$string .= '<track>';
				$string .= '<location>/tardes/cancons/';
				$string .= 	  $canco->fitxer;
				$string .= '</location>';
				$string .= '<creator>'.$canco->artista.'</creator>';
				$string .= '<album>'.$canco->album.'</album>';
				$string .= '<title>'.$canco->titol.'</title>';
				$string .= '<annotation></annotation>';
				$string .= '<image>';
				$string .= 	  'http://images.amazon.com/images/P/'.$canco->imatge;
				$string .= '</image>';
				$string .= '</track>';
			}
		$string .= '</trackList>';
		$string .= '</playlist>';

		$xml = new SimpleXMLElement($string);
        $xml->asXML($this->target_path . $this->xml . '.xspf');
    }
    
    public function afegirCanco($id)
    {
		$canco = new Mp3_GestionarCanco();
		$canco->cercarPerId($id);
        $this->cancons[$canco->id] = $canco;
    }
    
    public function borrarCanco($id)
    {
		if(array_key_exists($id, $this->cancons)) {
			unset($this->cancons[$id]);
		}
    }
    
    public function listCancons()
    {
		$titols = array();
		foreach($this->cancons as $canco) {
			$titols[$canco->id]['id'] = $canco->id;
			$titols[$canco->id]['titol'] = $canco->titol;
			$titols[$canco->id]['publica'] = $canco->publica;
		}
		return $titols;
    }
}
